package com.deep.transaction.demo7.service;

import com.deep.transaction.demo7.model.User;
import org.springframework.aop.framework.AopContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

@Service
public class UserService3 {


    @Autowired
    public ApplicationContext applicationContext;

    @Autowired
    @Qualifier("demo7UserService")
    @Lazy
    public UserService userService;

    /**
     * 事务自调用解决方式一：引入当前类实例
     *
     * @param user
     */
    public void test02Solution1(User user) {
        userService.testTransac(user);
    }

    /**
     * 事务自调用解决方式二：通过上下文获取当前类实例
     *
     * @param user
     */
    public void test02Solution2(User user) {
        UserService bean = applicationContext.getBean(UserService.class);
        bean.testTransac(user);
    }

    /**
     * 事务自调用解决方式三：通过aopContext获取当前代理对象
     *
     * @param user
     */
    public void test02Solution3(User user) {
        UserService context = (UserService) AopContext.currentProxy();
        context.testTransac(user);
    }
}
